package com.ews.onlineexchange.service.comparator;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import com.ews.onlineexchange.model.Trade;

/**
 * 评价分数计算机
 * 高分排位靠前
 */
public class ScoreComparator implements Comparator<Trade> {

	@Override
	public int compare(Trade arg0, Trade arg1) {		
		BigDecimal score0 = arg0.getSupplier().getCreditScore();
		BigDecimal score1 = arg1.getSupplier().getCreditScore();
		if (score1 == null) 
			return -1;
		
		if (score0 == null)
			return 1;
		
		int compareTo = score0.compareTo(score1);
		if (compareTo != 0)
			return compareTo * -1;
		else {
			int rand = new Random().nextInt(3) - 1;
			return rand;
		}
	}
	
	public static void main(String[] args) {
		final Random r = new Random();
		List<Integer> testList = Arrays.asList(new Integer[]{0, 1, 2, 3, 4});
		int count = 0;
		for (int i = 0; i < 10000; i++) {
			Collections.sort(testList, new Comparator<Integer>() {
				@Override
				public int compare(Integer o1, Integer o2) {
					int rand = r.nextInt(3) - 1;					
					return rand;
				}			
			});
			if (testList.get(0) == 0)
				count ++;
			System.out.println(testList);
		}
		
	}
}
